package ua.keeper.query;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class DocHListAdapter extends ArrayAdapter<DocHListItem>
{
	private ArrayList<DocHListItem> listItems_;
	private Context context_;
    /*****************************************************************************************/
	public DocHListAdapter(Context context, int textViewResourceId, ArrayList<DocHListItem> items)
	{
        super(context, textViewResourceId, items);
        context_ = context;
        listItems_ = items;
	}
	
    /*****************************************************************************************/
	@Override
    public View getView(int position, View convertView, ViewGroup parent)
	{
        View v = convertView;
        if (v == null)
        {
			LayoutInflater vi = (LayoutInflater) context_.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	        v = vi.inflate(R.layout.doc_h_list_row, null);
        }
        DocHListItem item = (DocHListItem) listItems_.get(position);
        if (item != null)
        {
        	if (item.getId() < 0)
        	{
        		LayoutInflater vi = (LayoutInflater) context_.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    	        v = vi.inflate(R.layout.doc_h_list_add, null);
        	}
        	else
        	{
                TextView tvTitle = (TextView) v.findViewById(R.id.docHTitleView);
                TextView tvDate = (TextView) v.findViewById(R.id.docHDateView);
                TextView tvTotal = (TextView) v.findViewById(R.id.docHTotalView);
                if (tvTitle != null)
                {
                	tvTitle.setText(item.getTitle());
                }
                if (tvDate != null)
                {
                	tvDate.setText("<fix date>");
                }
                if (tvTotal != null)
                {
                	tvTotal.setText(Double.toString(item.getTotalSumm()) + " (" +
                						Integer.toString(item.getTotalCount()) + ")"
                					);
                }
        	}
        }
        else
        {
			TextView tvTitle = (TextView) v.findViewById(R.id.docHTitleView);
			tvTitle.setText("<ERROR>");
		}
        return v;
    }
}